package com.qyhl.webtv.module_news.news.column;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.makeramen.roundedimageview.RoundedImageView;
import com.qyhl.cloud.webtv.module_news.R;
import com.qyhl.cloud.webtv.module_news.R2;
import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.news.UnionBean;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import butterknife.BindView;

public class ColumnNewsListFragment extends BaseFragment implements ColumnNewsListContract.ColumnNewsListView {
    @BindView(R2.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;

    private ColumnNewsListPresenter mPresenter;

    private CommonAdapter<UnionBean> commonAdapter;

    private List<UnionBean> mDataList = new ArrayList<>();

    private String sectionId;

    public void setSectionId(String sectionId) {
        this.sectionId = sectionId;
    }

    private int type;

    public void setType(int type) {
        this.type = type;
    }

    @SuppressLint("InflateParams")
    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.news_fragment_column_list, null);
    }

    @Override
    public void initImmersionBar() {

    }

    public static ColumnNewsListFragment newInstance(String sectionId) {
        ColumnNewsListFragment fragment = new ColumnNewsListFragment();
        fragment.setSectionId(sectionId);
        fragment.setType(0);
        return fragment;
    }

    public static ColumnNewsListFragment newInstance(String sectionId, int type) {
        ColumnNewsListFragment fragment = new ColumnNewsListFragment();
        fragment.setSectionId(sectionId);
        fragment.setType(type);
        return fragment;
    }

    @Override
    protected void create() {
        loadMask.setStatus(LoadingLayout.Loading);
        mPresenter = new ColumnNewsListPresenter(this);

        recycleView.setLayoutManager(new LinearLayoutManager(getContext()));

        DividerItemDecoration divider = new DividerItemDecoration(Objects.requireNonNull(getContext()), DividerItemDecoration.VERTICAL);
        divider.setDrawable(Objects.requireNonNull(ContextCompat.getDrawable(getContext(), R.drawable.news_item_leader_divider)));
        recycleView.addItemDecoration(divider);

        int layoutId;
        if (type == 0) {
            layoutId = R.layout.news_item_column_list;
        } else {
            layoutId = R.layout.news_item_column_list2;
        }

        recycleView.setAdapter(commonAdapter = new CommonAdapter<UnionBean>(getContext(), layoutId, mDataList) {
            @Override
            protected void convert(ViewHolder holder, UnionBean bean, int position) {
                holder.setText(R.id.title, bean.getName());

                RoundedImageView cover = holder.getView(R.id.cover);

                Glide.with(ColumnNewsListFragment.this)
                        .load(bean.getLogo())
                        .apply(new RequestOptions().placeholder(R.drawable.cover_large_default).error(R.drawable.cover_large_default))
                        .into(cover);
            }
        });

        mPresenter.getList(sectionId);

    }

    @Override
    protected void setListener() {
        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadMask.setReloadButtonText("加载中...");
                mPresenter.getList(sectionId);
            }
        });

        commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("id", mDataList.get(position).getId() + "");
                bundle.putString("cover", mDataList.get(position).getLogo());
                bundle.putString("title", mDataList.get(position).getName());
                RouterManager.router(ARouterPathConstant.SD_COLUMN_LIST, bundle);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });

    }

    @Override
    protected void onFirstVisible() {

    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }

    @Override
    public void setList(List<UnionBean> beanList) {
        loadMask.setReloadButtonText("点击重试~");
        loadMask.setStatus(LoadingLayout.Success);

        mDataList.clear();
        mDataList.addAll(beanList);

        commonAdapter.notifyDataSetChanged();
    }

    @Override
    public void setEmpty(String msg) {
        loadMask.setEmptyText(msg);
        loadMask.setStatus(LoadingLayout.Empty);
    }

    @Override
    public void setNetError(String msg) {
        loadMask.setNoNetworkText(msg);
        loadMask.setStatus(LoadingLayout.No_Network);
    }

    @Override
    public void setError(String msg) {
        loadMask.setErrorText(msg);
        loadMask.setStatus(LoadingLayout.Error);
    }
}
